#include "Tank.h"
#include <string.h>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <list>
#include <vector>
using namespace std;
//请勿修改以上头文件
/*

您可以在这里添加您所需头文件

*/
#include "core.h"
#include "utils.h"

vector<int> getEnemyInRange(){
	vector<int> enemeyInRange;
	int tankNum;
	updateField(gdata);
	short range = gdata.tank[gdata.myID].range;
	short row = gdata.tank[gdata.myID].row;
	short col = gdata.tank[gdata.myID].col;
	short enemyMinTank = 5;
	short enemyMaxTank = 9;
	if(gdata.myFlag==BLUE){
		enemyMinTank = 0;
		enemyMaxTank = 4;
	}

	for(int i=-1*range;i<=range;i++)
		for(int j=-1*range;j<=range;j++)
			 if((abs(i)+abs(j)<=range)&&(IsOk(row+i,col+j))&&(Field[row+i][col+j])&&
					 ((enemyMinTank<=gdata.map[row+i][col+j].whoIsHere)&&(gdata.map[row+i][col+j].whoIsHere<=enemyMaxTank))){
				 tankNum = gdata.map[row+i][col+j].whoIsHere;
				 enemeyInRange.push_back(tankNum);
			 }
  return enemeyInRange;
}

TankData getEnemyTankDataAtPoint(short row, short col){
	TankData tankData;
	tankData.ID=-1;
	short enemyMin = 5;
	short enemyMax = 9;
	if(gdata.myFlag==BLUE) {
		enemyMin = 0;
		enemyMax = 4;
	}
	if(IsOk(row,col)) {
	   short num = gdata.map[row][col].whoIsHere;
	   if(enemyMin<=num&&num<=enemyMax){
		   tankData = gdata.tank[num];
	   }
	}
	return tankData;
}
list<Choice> doSpecialAttack(){
	list<Choice> ls;
	short row = gdata.tank[gdata.myID].row;
	short col = gdata.tank[gdata.myID].col;
	TankData weakTankData;
	weakTankData.ID=-1;
	weakTankData.life=10;
	short flag=0;
	TankData tankData = getEnemyTankDataAtPoint(row-1,col);
	if(tankData.life<weakTankData.life) { weakTankData = tankData; flag=1;}
	tankData = getEnemyTankDataAtPoint(row+1,col);
	if(tankData.life<weakTankData.life) { weakTankData = tankData;flag=2;}
	tankData = getEnemyTankDataAtPoint(row,col-1);
	if(tankData.life<weakTankData.life) { weakTankData = tankData;flag=3;}
	tankData = getEnemyTankDataAtPoint(row,col+1);
	if(tankData.life<weakTankData.life) { weakTankData = tankData;flag=4;}

    if(weakTankData.ID!=-1){ //周围四处有敌人
    	TankData myData = gdata.tank[gdata.myID];
    	bool IFGO=0;
    	if(weakTankData.life<myData.life&&weakTankData.attack>=myData.attack) //暂时只考虑可撞死敌人的情况
    		IFGO=1;
    	else if(weakTankData.attack>myData.attack){ //敌人攻击力强
    		IFGO=1;
    	}
    	else {
    		//以后补上
    	}
    	if(IFGO){
    		Order order;
    		switch (flag){
    		case 1: order.type = GOUP;break;
    		case 2: order.type = GODOWN;break;
    		case 3: order.type = GOLEFT;break;
    		case 4: order.type = GORIGHT; break;
    		default : order.type = STOP; break;
    		}
    	   Choice choice;
    	   choice.order = order;
    	   choice.takeSourceStep=1;
    	   choice.willKill=(weakTankData.life<myData.life); // 暂时的情况，可以kill
    	   choice.willDie=(weakTankData.life<myData.life);
    	   ls.push_back(choice);
    	}
    }

	return ls;
}
list<Choice> makeChoiceByAttack(DataForAI& data) {
    list<Choice> ls;
    ls = doSpecialAttack();
    if(!ls.empty())
    	{
    	fout<<"doSpcialAttack"<<endl;
    	return ls;
    	}

    Choice choice;
    vector<int> enemyInRange = getEnemyInRange();

 if(enemyInRange.size()){ //有敌人
        short attack = gdata.tank[gdata.myID].attack;
      //  short life = gdata.tank[gdata.myID].life;
    	choice.willDie = willDieAtPoint(gdata.tank[gdata.myID].row,gdata.tank[gdata.myID].col,gdata.tank[gdata.myID].life);
      short minEnemyLife=9;
      short minLifeEnemyNum = enemyInRange[0];
      short maxEnemyAttack=1;
      short maxAttackEnemyNum = enemyInRange[0];
      short maxEnemyRange=1;
      short maxRangeEnemyNum = enemyInRange[0];
    for(vector<int>::iterator it=enemyInRange.begin();it!=enemyInRange.end();it++){
    	short enemyTankNum = *it;
    	short enemyAttack = gdata.tank[enemyTankNum].attack;
    	short enemyLife = gdata.tank[enemyTankNum].life;
    	short enemyRange = gdata.tank[enemyTankNum].range;
        if(enemyLife<minEnemyLife)  {
        	minEnemyLife = enemyLife;
        	minLifeEnemyNum = enemyTankNum;
        }
        if(enemyAttack>maxEnemyAttack){
        	maxEnemyAttack= enemyAttack;
        	maxAttackEnemyNum = enemyTankNum;
        }
        if(enemyRange>maxEnemyRange){
        	maxEnemyRange = enemyRange;
        	maxRangeEnemyNum = enemyTankNum;
        }
    } //for

    short objEnemyNum = minLifeEnemyNum;
    if(gdata.tank[maxAttackEnemyNum].life==gdata.tank[objEnemyNum].life)
    	     { objEnemyNum = maxAttackEnemyNum;
             if((gdata.tank[maxRangeEnemyNum].life==gdata.tank[objEnemyNum].life)&&(gdata.tank[maxRangeEnemyNum].attack==gdata.tank[objEnemyNum].attack))
            	 objEnemyNum = maxRangeEnemyNum;
             }
    else if(gdata.tank[maxRangeEnemyNum].life==gdata.tank[objEnemyNum].life)
    	   objEnemyNum = maxRangeEnemyNum;

    if(attack>=gdata.tank[objEnemyNum].life) { // 杀死
    	choice.order.type=FIRE;
    	choice.order.row= gdata.tank[objEnemyNum].row;
    	choice.order.col= gdata.tank[objEnemyNum].col;
    	choice.willKill=1;
    	choice.takeSourceStep=1;
    	ls.push_back(choice);
    	}
    else { //杀不死
    	choice.order.type=FIRE;
    	choice.order.row= gdata.tank[objEnemyNum].row;
    	choice.order.col= gdata.tank[objEnemyNum].col;
    	choice.willKill=0;
    	choice.takeSourceStep=2;
    	ls.push_back(choice);
    }

}else{ //射程内，没有敌人
    choice.order.type=STOP;
    choice.takeSourceStep=1000; //为了降低权重
    choice.willDie=willDieAtPoint(gdata.tank[gdata.myID].row,gdata.tank[gdata.myID].col,gdata.tank[gdata.myID].life);;
    choice.willKill=0;
    ls.push_back(choice);
}
    return ls;
}
